/**
 *  Copyright 2006-2007 EmbedChat
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.embedchat.desktop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.embedchat.protocol.message.server.RoomsListMessage.RoomDescription;
import org.embedchat.protocol.message.server.UsersListMessage.UserDescription;

public class StateModel {
	
	private List<Observer> observers = new ArrayList<Observer>();
	
	public static final int DISCONNECTED_STATE = 1;
	public static final int CONNECTION_ESTABLISHED_STATE = 2;
	public static final int USER_LOGGED_STATE = 3;
	
	private int modelState = DISCONNECTED_STATE;

	private boolean requestForConnect = false;
	private boolean requestForLogin = false;
	private boolean requestForError = false;
	private boolean requestForDisconnect = false;
	private boolean requestForReconnect = false;
	private boolean requestForSendMessage = false;
	private boolean requestForShowMessage = false;
	
	private String sendMessage;
	private String receivedMessage;
	private String receivedMessageAuthor;
	private String errorMessage;
	
	/*
	 * When user logged, this variables store his login name and password.
	 */
	private String loginName = "";
	private String password = "";
	private String selectedRoomName = "";
	
	private Integer selectedRoomId = -1;

	private List<UserDescription> roomUsers = new ArrayList<UserDescription>();
	private Map<Integer, RoomDescription> rooms = new HashMap<Integer, RoomDescription>();
	
	private String connectionHost = "localhost";
	private int connectionPort = 49177;
	
	public StateModel() {
	}
	
	public void addObserver(Observer observer) {
		if(!observers.contains(observer)) {
			observers.add(observer);
		}
	}
	
	public void removeObserver(Observer observer) {
		if(observers.contains(observer)) {
			observers.remove(observer);
		}
	}
	
	public void update() {
        Thread thread = new Thread() {
            public void run() {
        		for (Observer observer : observers) {
        			observer.update();
        		}
            }
        };
        thread.setDaemon(true);
        thread.start();
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isRequestForConnect() {
		return requestForConnect;
	}

	public void setRequestForConnect(boolean requestForConnect) {
		this.requestForConnect = requestForConnect;
	}

	public boolean isRequestForLogin() {
		return requestForLogin;
	}

	public void setRequestForLogin(boolean requestForLogin) {
		this.requestForLogin = requestForLogin;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public boolean isRequestForError() {
		return requestForError;
	}

	public void setRequestForError(boolean requestForError) {
		this.requestForError = requestForError;
	}

	public boolean isRequestForDisconnect() {
		return requestForDisconnect;
	}

	public void setRequestForDisconnect(boolean requestForDisconnect) {
		this.requestForDisconnect = requestForDisconnect;
	}

	public List<UserDescription> getRoomUsers() {
		return roomUsers;
	}

	public void setRoomUsers(List<UserDescription> roomUsers) {
		this.roomUsers = roomUsers;
		update();
	}

	public boolean isRequestForSendMessage() {
		return requestForSendMessage;
	}

	public void setRequestForSendMessage(boolean requestForSend) {
		this.requestForSendMessage = requestForSend;
		update();		
	}

	public boolean isRequestForReconnect() {
		return requestForReconnect;
	}

	public void setRequestForReconnect(boolean requestForReconnect) {
		this.modelState = DISCONNECTED_STATE;
		this.requestForReconnect = requestForReconnect;
		update();		
	}

	public String getSendMessage() {
		return sendMessage;
	}

	public void setSendMessage(String sendMessage) {
		this.sendMessage = sendMessage;
	}

	public String getReceivedMessage() {
		return receivedMessage;
	}

	public void setReceivedMessage(String receivedMessage) {
		this.receivedMessage = receivedMessage;
	}

	public String getReceivedMessageAuthor() {
		return receivedMessageAuthor;
	}

	public void setReceivedMessageAuthor(String receivedMessageAuthor) {
		this.receivedMessageAuthor = receivedMessageAuthor;
	}

	public boolean isRequestForShowMessage() {
		return requestForShowMessage;
	}

	public void setRequestForShowMessage(boolean requestForShowMessage) {
		this.requestForShowMessage = requestForShowMessage;
	}

	public Map<Integer, RoomDescription> getRooms() {
		return rooms;
	}

	public void setRooms(List<RoomDescription> roomsList) {
		for(int i = 0; i < roomsList.size(); i++) {
			rooms.put(new Integer(i), roomsList.get(i));
		}
	}

	public Integer getSelectedRoomId() {
		return selectedRoomId;
	}

	public void setSelectedRoomId(Integer selectedRoomId) {
		this.selectedRoomId = rooms.get(selectedRoomId).getRoomId();
	}

	public String getSelectedRoomName() {
		return selectedRoomName;
	}

	public void setSelectedRoomName(String selectedRoomName) {
		this.selectedRoomName = selectedRoomName;
	}

	public String getConnectionHost() {
		return connectionHost;
	}

	public int getConnectionPort() {
		return connectionPort;
	}

	public void setConnectionParameters(String connectionHost, int connectionPort) {
		this.connectionHost = connectionHost;
		this.connectionPort = connectionPort;
	}
	
	public void changeState(int modelState) {
		this.modelState = modelState;
	}
	
	public int getModelState() {
		return modelState;
	}
}
